package ciyuan.Player;

import ciyuan.Constants.PlayerDataType;
import ciyuan.DataManager.PlayerDataManager;
import ciyuan.Effect.Effects;
import ciyuan.Tasks.PlayerTaskManager;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.util.DamageSource;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

/**
 * @author 150149
 */

@Mod.EventBusSubscriber
public class WaterDecrease {

    static {
        PlayerTaskManager.register(player -> {
            int water = PlayerDataManager.getInt(player, PlayerDataType.WATER);

            if (water > 0 && !player.isCreative()) {
                if (player.hasEffect(Effects.thirsty)) {
                    PlayerDataManager.setServerPlayerData(player, PlayerDataType.WATER, water - 300);
                } else {
                    PlayerDataManager.setServerPlayerData(player, PlayerDataType.WATER, water - 3);
                }
            } else if (water < 0) {
                PlayerDataManager.setServerPlayerData(player, PlayerDataType.WATER, 0);
            } else if (player.isAlive() && !player.isInvulnerable()) {
                player.hurt(DamageSource.MAGIC, player.getMaxHealth() / 10);
            }
        });
    }

    @SubscribeEvent
    public static void onPlayerRespawn(PlayerEvent.PlayerRespawnEvent event) {
        if (event.getPlayer() instanceof ClientPlayerEntity) {
            return;
        }

        PlayerDataManager.setServerPlayerData(event.getPlayer(), PlayerDataType.WATER, 10000);
        PlayerDataManager.setServerPlayerData(event.getPlayer(), PlayerDataType.TEMPERATURE, 37.0);
    }

}
